package com.baibei.order.detail;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.SparseArray;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.ebec.quotation.AppQuotationFilter;
import com.baibei.ebec.quotation.QuotationManager;
import com.baibei.ebec.quotation.event.IQuotationEvent;
import com.baibei.ebec.sdk.ApiConstant;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IOrderApi;
import com.baibei.ebec.sdk.ITradeApi;
import com.baibei.model.QuotationInfo;
import com.baibei.model.TradeWebSocketInfo;
import com.baibei.quotation.QuotationFilter;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.Empty;
import com.blankj.utilcode.utils.LogUtils;

import org.greenrobot.eventbus.Subscribe;

/**
 * 订单详情表现层实现
 * Created by shenminjie on 2017/8/11.
 * mail:shenminjie@100bei.com
 */

public class OrderDetailPresenterImpl extends BasicPresenterImpl<OrderDetailContract.OrderDetailView> implements OrderDetailContract.Presenter, IQuotationEvent {

    /**
     * 商品交易接口
     */
    private ITradeApi mApi;

    /**
     * 用于调用退订接口后，等待平仓推送过程的一个倒计时
     * 倒计时超时
     */
    private CountDownTimer mCountDownTimer = new CountDownTimer(5000, 1000) {
        @Override
        public void onTick(long l) {
        }

        @Override
        public void onFinish() {
            mView.dismissLoading();
            mView.onUnsubscribeTimeout();
        }
    };
    private final IOrderApi mOrderApi;

    public OrderDetailPresenterImpl(Context context, OrderDetailContract.OrderDetailView view) {
        super(context, view);
        mApi = ApiFactory.getInstance().getTradeApi();
        mOrderApi = ApiFactory.getInstance().getOrderApi();

        mView.initView();
        QuotationFilter filter = new QuotationFilter(AppQuotationFilter.ACTION_TRADE_CLOSED);
        filter.addAction(AppQuotationFilter.ACTION_TRADE_CREATED);
        filter.addAction(QuotationFilter.ACTION_QUOTATION_PRODUCT);
        QuotationManager.getInstance().register(this, filter);
    }

    @Subscribe
    @Override
    public void onEvent(SparseArray<QuotationInfo> quotations) {
        mView.refresh(quotations);
    }

    @Override
    public void destroy() {
        QuotationManager.getInstance().unregister(this);
        //清空倒计时
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
        super.destroy();
    }

    @Override
    public void unsubscribeProduct() {
        //退订
        mView.showLoading();
        createObservable(mApi.cancel(mView.getOrderId(), mView.getArea())).subscribe(new ApiDefaultObserver<Empty>() {
            @Override
            protected void onError(String s) {
                mView.dismissLoading();
                mView.onLoadFailed(s);
            }

            @Override
            protected void accept(Empty empty) {
                LogUtils.e(empty);
                //开始倒计时等待推送，退订问题
                mCountDownTimer.cancel();
                mCountDownTimer.start();
            }
        });
    }




    @Subscribe
    public void onEvent(TradeWebSocketInfo tradeInfo) {
        mCountDownTimer.cancel();
        mView.dismissLoading();
        mView.onUnsubscribeSuccess(tradeInfo.getMsg());
    }
}
